package com.ruoyi.web.qualityController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.quality.QualityDefectProject;
import com.ruoyi.system.mapper.QualityDefectProjectMapper;
import com.ruoyi.system.service.IQualityDefectProjectService;
import com.ruoyi.system.service.IQualityDefectTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 质量质检项目维护Controller
 *
 * @author ruoyi
 * @date 2023-03-08
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
        @Api(tags ="质量质检项目维护")
    @RestController
@RequestMapping("/quality/project" )
public class QualityDefectProjectController extends BaseController {

    private final IQualityDefectProjectService iQualityDefectProjectService;

    @Autowired
    IQualityDefectTypeService iQualityDefectTypeService;

    @Resource
    private QualityDefectProjectMapper qualityDefectProjectMapper;

    /**
     * 查询质量质检项目维护列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:list')")
    @GetMapping("/list")
        @ApiOperation(value = "查询质量质检项目维护列表")
        public TableDataInfo list(QualityDefectProject qualityDefectProject)
    {
        startPage();
        LambdaQueryWrapper<QualityDefectProject> lqw = new LambdaQueryWrapper<QualityDefectProject>();
        if (StringUtils.isNotBlank(qualityDefectProject.getCode())){
            lqw.like(QualityDefectProject::getCode ,qualityDefectProject.getCode());
        }
        if (StringUtils.isNotBlank(qualityDefectProject.getName())){
            lqw.like(QualityDefectProject::getName ,qualityDefectProject.getName());
        }
        if (qualityDefectProject.getCheckTypeId()!=null){
            lqw.eq(QualityDefectProject::getCheckTypeId ,qualityDefectProject.getCheckTypeId());
        }
        if (qualityDefectProject.getDel() != null){
            lqw.eq(QualityDefectProject::getDel ,qualityDefectProject.getDel());
        }
        if (qualityDefectProject.getDataType() != null){
            lqw.eq(QualityDefectProject::getDataType ,qualityDefectProject.getDataType());
        }
        if (StringUtils.isNotBlank(qualityDefectProject.getUnit())){
            lqw.like(QualityDefectProject::getUnit ,qualityDefectProject.getUnit());
        }
        if (StringUtils.isNotBlank(qualityDefectProject.getCheckTypeName())){
            lqw.apply("b.dict_value like '%"+qualityDefectProject.getCheckTypeName()+"%'");
        }

        List<QualityDefectProject> list = qualityDefectProjectMapper.listByParam(lqw);
        return getDataTable(list);
    }

    /**
     * 导出质量质检项目维护列表
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:export')" )
    @Log(title = "质量质检项目维护" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
                @ApiOperation(value = "导出质量质检项目维护列表")
            public AjaxResult export(QualityDefectProject qualityDefectProject) {
        LambdaQueryWrapper<QualityDefectProject> lqw = new LambdaQueryWrapper<QualityDefectProject>(qualityDefectProject);
        List<QualityDefectProject> list = iQualityDefectProjectService.list(lqw);
        ExcelUtil<QualityDefectProject> util = new ExcelUtil<QualityDefectProject>(QualityDefectProject. class);
        return util.exportExcel(list, "project" );
    }

    /**
     * 获取质量质检项目维护详细信息
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:query')" )
    @GetMapping(value = "/{id}" )
                @ApiOperation(value = "获取质量质检项目维护详细信息")
            public AjaxResult getInfo(@PathVariable("id" ) String id) {
        return AjaxResult.success(iQualityDefectProjectService.getById(id));
    }

    /**
     * 新增质量质检项目维护
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:add')" )
    @Log(title = "质量质检项目维护" , businessType = BusinessType.INSERT)
    @PostMapping
                @ApiOperation(value = "新增质量质检项目维护")
            public AjaxResult add(@RequestBody QualityDefectProject qualityDefectProject) {
        iQualityDefectTypeService.checkUnique("quality_defect_project",qualityDefectProject.getCode(),null);
        return toAjax(iQualityDefectProjectService.save(qualityDefectProject) ? 1 : 0);
    }

    /**
     * 修改质量质检项目维护
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:edit')" )
    @Log(title = "质量质检项目维护" , businessType = BusinessType.UPDATE)
    @PutMapping
                @ApiOperation(value = "修改质量质检项目维护")
            public AjaxResult edit(@RequestBody QualityDefectProject qualityDefectProject) {
        iQualityDefectTypeService.checkUnique("quality_defect_project",qualityDefectProject.getCode(),qualityDefectProject.getId());
        return toAjax(iQualityDefectProjectService.updateById(qualityDefectProject) ? 1 : 0);
    }

    /**
     * 删除质量质检项目维护
     */
    //@PreAuthorize("@ss.hasPermi('quality:project:remove')" )
    @Log(title = "质量质检项目维护" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{codes}" )
                @ApiOperation(value = "删除质量质检项目维护")
            public AjaxResult remove(@PathVariable String[] codes) {
        return toAjax(iQualityDefectProjectService.removeByIds(Arrays.asList(codes)) ? 1 : 0);
    }
}
